import random
from fight.fightUtil import fighting
from operate import left_click, pic_click
from util import go_position, confirm_step, go_back, go_home
from variable import Variable
from time import sleep

fight_number = 0
add_role = True


def skip():
    left_click(Variable.width / 1.139, Variable.height / 8.513, delay=1)
    pic_click("./images/fight/fight_three/ok.png", delay=1)


# 去节点选择界面
def goMap():
    if confirm_step("./images/fight/fight_three/team.png"):
        return True
    else:
        if not confirm_step("./images/fight/fight_three/position_fight_three.png"):
            go_position("fight_three")
            goMap()
        else:
            left_click(Variable.width / 1.216, Variable.height / 1.119)
            # 未完待续
            if pic_click("./images/fight/fight_three/未完待续.png"):
                return True
            elif confirm_step("./images/fight/fight_three/team.png"):
                return True
            else:
                left_click(Variable.width / 7.882, Variable.height / 2.862, delay=0.3)
                left_click(Variable.width / 1.554, Variable.height / 1.12, delay=1)
                pic_click("./images/fight/fight_three/浪漫作品.png")
                pic_click("./images/fight/fight_three/use.png", delay=1)
                pic_click("./images/fight/fight_three/选定难度.png", delay=1)
                pic_click("./images/fight/fight_three/ok.png", delay=6)
                # skip
                skip()
                # 选人
                chooseRoles()


def go_fight(type=""):
    global add_role
    if add_role:
        for i in [
            [4.77, 2.08, 4.41, 3.64],
            [2.1, 1.633, 4.41, 2.248],
            # [1.4, 1.72, 4.41, 1.616],
        ]:
            left_click(Variable.width / i[0], Variable.height / i[1], delay=1)
            left_click(Variable.width / i[2], Variable.height / i[3], delay=1)
            if confirm_step("./images/fight/fight_three/卸下队伍.png", confidence=0.95):
                go_back()
                continue
            left_click(Variable.width / 1.243, Variable.height / 1.435, delay=1)
            if confirm_step("./images/fight/fight_three/标记提示.png"):
                left_click(Variable.width / 1.90175, Variable.height / 1.1787)
        add_role = False

    left_click(Variable.width / 1.106, Variable.height / 1.9415, delay=1)
    if pic_click("./images/fight/fight_three/作战开始.png"):
        fighting()
        sleep(2)
        while confirm_step(
            "images/fight/pause.png",
            confidence=0.8,
            w=100,
            h=70,
            bl=1550,
            bt=90,
            imgsrc="images/current1.png",
        ):
            fighting()
            sleep(2)
        while confirm_step("./images/fight/fight_three/获得奖励.png"):
            print("获得奖励")
            sleep(0.5)
            go_back()
            sleep(0.5)
        if type == "辞本":
            go_back()
        if type == "打第一个" or type == "浪漫":
            print("选择增益")
            sleep(1)
            y = random.choice([3.376, 2.016, 1.39])
            left_click(Variable.width / 1.575, Variable.height / y, delay=1)
            left_click(Variable.width / 1.227, Variable.height / 1.124, delay=2)
            # pic_click("./images/fight/fight_three/ok.png", delay=1)
            sleep(1)


def chooseRoles():
    global add_role
    left_click(Variable.width / 4.866, Variable.height / 1.879, delay=1)
    pic_click("./images/fight/fight_three/招募.png")
    sleep(2)
    left_click(Variable.width / 2.09, Variable.height / 1.879, delay=1)
    pic_click("./images/fight/fight_three/招募.png")
    sleep(2)
    pic_click("./images/fight/fight_three/选定演员.png", delay=1)
    add_role = True


def fightThree():
    print("开始宣叙妄想")
    global fight_number
    fight_number = 0
    while fight_number < Variable.fight_three["number"]:
        goMap()
        # 有确认选择
        if pic_click("./images/fight/fight_three/确认选择.png"):
            go_fight("fight")
            continue
        else:
            if confirm_step("./images/fight/fight_three/战斗冲突.png"):
                for i in ["辞本", "灵感", "浪漫", "打第一个"]:
                    if i == "打第一个":
                        xy = [Variable.width / 2.55, Variable.height / 1.688]
                        x = xy[0]
                        y = xy[1]
                    else:
                        xy = confirm_step(
                            "./images/fight/fight_three/" + i + ".png", confidence=0.95
                        )
                    if xy:
                        print(i)
                        if i != "打第一个":
                            xy = xy["result"]
                            x = xy[0] / Variable.scale
                            y = xy[1] / Variable.scale + 200 / Variable.scale
                        left_click(x, y, delay=1)
                        pic_click("./images/fight/fight_three/确认选择.png")
                        go_fight(i)
                        break
            elif confirm_step("./images/fight/fight_three/选定演员.png"):
                fight_number -= 1
                chooseRoles()
            else:
                for i in ["密林深处", "即兴排演", "魔法", "幕间准备", "花与羽毛", "登船"]:
                    xy = confirm_step(
                        "./images/fight/fight_three/" + i + ".png", confidence=0.95
                    )
                    if not xy:
                        continue
                    print(i)
                    left_click(Variable.width / 2.08, Variable.height / 1.688, delay=1)
                    pic_click("./images/fight/fight_three/确认选择.png")

                    if i == "密林深处":
                        left_click(
                            Variable.width / 1.544, Variable.height / 1.571, delay=1
                        )
                        pic_click("./images/fight/fight_three/下一步.png", delay=1)
                        left_click(
                            Variable.width / 1.22, Variable.height / 1.118, delay=1
                        )

                    elif i == "花与羽毛":
                        while pic_click(
                            "./images/fight/fight_three/下一步.png", delay=0.5
                        ):
                            sleep(0.5)
                        sleep(3)
                        skip()
                        sleep(5)
                        skip()
                        chooseRoles()

                    elif i == "即兴排演":
                        go_fight("fight")

                    elif i == "登船":
                        if not pic_click("./images/fight/fight_three/下一步.png"):
                            left_click(
                                Variable.width / 1.544, Variable.height / 1.37, delay=1
                            )

                    elif i == "魔法":
                        while not pic_click("./images/fight/fight_three/离开.png"):
                            left_click(
                                Variable.width / 1.544, Variable.height / 1.37, delay=1
                            )
                            pic_click("./images/fight/fight_three/下一步.png", delay=1)

                    elif i == "幕间准备":
                        xy = confirm_step("./images/fight/fight_three/马车灵感.png")
                        xy = xy or confirm_step("./images/fight/fight_three/华彩段.png")
                        if xy:
                            xy = xy["result"]
                            left_click(
                                xy[0] / Variable.scale, (xy[1] + 150) / Variable.scale
                            )
                            pic_click("./images/fight/fight_three/ok.png", delay=1)
                            go_back()
                            left_click(
                                Variable.width / 1.209, Variable.height / 1.128, delay=1
                            )
                            pic_click("./images/fight/fight_three/ok.png", delay=1)
                            left_click(
                                Variable.width / 1.209, Variable.height / 1.128, delay=1
                            )
                    break
        fight_number += 1

    print("宣叙妄想完成")
    go_home()


if __name__ == "__main__":
    fightThree()
